"""
auth:xgt-python
datetime:2021/11/11
爬取懂车帝二手车数据
"""
import pprint  # 格式化打印模块

import requests
import parsel
import csv

url = 'https://www.dongchedi.com/usedcar/x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-1-x-x-x-x-x'
headers = {
    'cookie': '_ga=GA1.2.2080831584.1636641588; _gid=GA1.2.1353747830.1636641588; ttwid=1%7C5VEm2p3YoTqEa2D-TrUUWGywo9iAdiJdnpGx8DpCkjw%7C1636641618%7C6c8b7fc54f09d1d7403d10ddb5588ef1fae23ad2f8e79f3494274ea0cd501009; tt_webid=7029322196641416708; tt_web_version=new; UM_distinctid=17d0f714558cc-0c1bfd58add0fc-57b1a33-144000-17d0f71455972e; _tea_utm_cache_1839=undefined; MONITOR_WEB_ID=c628df0c-7e51-4132-a3ed-7f1992b29fb5; city_name=%E9%93%9C%E9%99%B5; is_dev=false; is_boe=false; Hm_lvt_3e79ab9e4da287b5752d8048743b95e6=1636641621,1636646425,1636676498; Hm_lpvt_3e79ab9e4da287b5752d8048743b95e6=1636676501; CNZZDATA1278124308=1913161000-1636637191-https%253A%252F%252Fwww.baidu.com%252F%7C1636668250; _gat_gtag_UA_138671306_1=1',
    'referer': 'https://www.dongchedi.com/',
    'sec-ch-ua': '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'document',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-user': '?1',
    'upgrade-insecure-requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
}
response = requests.get(url, headers=headers)

html_data = response.text  # 格式化打印
# 提取数据(筛选) 在网页源代码里面去筛选我们需要的一些内容
selector = parsel.Selector(html_data)
# ul 标签下的 所有 li标签 里面存放着 我们所有需要的车辆信息
lis = selector.css('#__next > div:nth-child(2) > div.new-main.new > div > div > div.wrap > ul li')
for li in lis:
    # ::text 提取标签文本内容
    title = li.css('a dl dt p::text').get()
    # print(title)
    info = li.css('a dl dd:nth-child(2)::text').getall()
    info_str = ''.join(info)
    # print(info_str)
    # 字符串分割
    info_list = info_str.split('|')
    # 车辆年龄
    car_age = info_list[0].strip()
    # 行驶年龄
    mileage = info_list[1].strip().replace('万公里', '')
    # 城市
    city = info_list[2].strip()
    # 单个li 标签里面的 dd标签
    # 链接
    # 属性选择器
    link = 'https://www.dongchedi.com' + li.css('a::attr(href)').get()
    dds = li.css('a dl dd')
    # 如果当前 有 4个dd标签
    if len(dds) == 4:
        # 有认证的提取规则
        dcd_auth = li.css('a dl dd:nth-child(3) span::text').get()
        price = li.css('a dl dd:nth-child(4)::text').get()
        original_price = li.css('a dl dd:nth-child(5)::text').get()
    else:
        # 没有认证的提取规则
        dcd_auth = '无认证'
        price = li.css('a dl dd:nth-child(3)::text').get()
        original_price = li.css('a dl dd:nth-child(4)::text').get
    print(title, car_age, city, dcd_auth,mileage,original_price,link)

# 保存数据
    with open('二手车数据.csv',mode='a',encoding='utf-8',newline='') as file:
        csv_write = csv.writer(file)
        csv_write.writerow([title,car_age,city,dcd_auth,mileage,original_price,link])